package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @BelongsProject: itcast_health
 * @BelongsPackage: com.itheima.health.controller
 * @Author: ${Shiu}
 * @CreateTime: 2020-02-22 22:21
 * @Description: 菜单控制层
 */
@Slf4j
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;


    // @PreAuthorize("hasAuthority('MENU_EDIT')")
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        log.debug("menu ==={}" + menu);
        try {
            menuService.edit(menu);

            return new Result(true, MessageConst.ACTION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();

            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }
    // @PreAuthorize("hasAuthority('MENU_QUERY')")
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Menu menu = menuService.findById(id);
            System.out.println("=========================="+menu);
            return new Result(true, MessageConst.ACTION_SUCCESS,menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }
    // @PreAuthorize("hasAuthority('MENU_DELETE')")
    @RequestMapping("/delete")
    public Result  delete(Integer id){
        try {
            menuService.delete(id);
            return new Result(true, "删除菜单成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除菜单失败!");
        }
    }

    // @PreAuthorize("hasAuthority('MENU_ADD')")
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu) {
        try {
            menuService.add(menu);
            return new Result(true, "新建菜单成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新建菜单失败!");
        }
    }

    // @PreAuthorize("hasAuthority('MENU_QUERY')")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        log.debug("queryPageBean ==={}", queryPageBean);

        try {
            PageResult pageResult = menuService.pageQuery(queryPageBean);
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(0L, null);
        }
    }



    /**
     * 查询所有菜单
     *
     * @return
     */
   // @PreAuthorize("hasAuthority('MENU_QUERY')")
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Menu> menuList = menuService.findAll();
            return new Result(true, MessageConst.QUERY_MENU_SUCCESS, menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.QUERY_MENU_FAIL);
        }

    }
}
